
define(
	["require", "jquery", "bip.cache", "bip.base", "bip.async",
	 "datatables","datatables_bootstrap"
	],
	function(require){
		"use strict";
		
		var $ = require("jquery");
	    var $bip = require("bip.base");
	    
	    function extDetailToHeader(data,isRowObject){
	    	var detail = "<div class='bip-detail icon_open'></div>";
	    	
	    	extDTRowDetailToHeader(data, detail, isRowObject);
	    }
	    
	    //operatorArray is 2D
	    //structure : [[name,displayName,callback],[name,displayName,callback]]
	    //example:
	    //single element:[["editor","编辑",callback1]],  multiple element:[["editor", "删除", callback1],["del",callback2]]
	    function extOpToTail(data, operatorArray, isRowObject){
	    	var operator = "<div>";
	    	
	    	$.each(operatorArray, function(index, item){
	    		operator += "<a href=\"javascript:void(0);\" name=\"" + item[0] + "\" class=\"bip-operator\">" + item[1] + "</a>";
	    	});
	    	operator += "</div>";
	    	
	    	extDTRowOpToTail(data, operator, isRowObject);
	    }
	    
	    function extDetailAndOp(data, operatorArray, isRowObject){
	    	extDetailToHeader(data, isRowObject);
	    	extOpToTail(data, operatorArray, isRowObject);
	    }
	    
	    function extDTRowDetailToHeader(data, detail, isRowObject){
	    	if(isRowObject){
	    		if($.isArray(data)){
	    			$.each(data, function(index, item){
			    		$.extend(item, {"detail":detail});
			    	});
	    		}else{
	    			$.extend(data, {"detail":detail});
	    		}
	    	}else{
	    		if(data[0] && $.isArray(data[0])){
	    			$.each(data, function(index, item){
			    		item.unshift(detail);
			    	});
	    		}else{
	    			data.unshift(detail);
	    		}
	    	}
	    }
	    
	    function extDTRowOpToTail(data, operator, isRowObject){
	    	if(isRowObject){
	    		if($.isArray(data)){
	    			$.each(data, function(index, item){
			    		$.extend(item, {"operator":operator});
			    	});
	    		}else{
	    			$.extend(data, {"operator":operator});
	    		}
	    	}else{
	    		if(data[0] && $.isArray(data[0])){
	    			$.each(data, function(index, item){
			    		item.push(operator);
			    	});
	    		}else{
	    			data.push(operator);
	    		}
	    	}
	    }
	    
	    function initDataTable(tableID, oSetting, operatorArray, showDetailCallback, showDetailTmpName){
	    	var thisTable = $("#" + tableID).dataTable(oSetting);
	    	bindDetailAndOperatorEvent(tableID, operatorArray, showDetailCallback, showDetailTmpName, thisTable);
	    	return thisTable;
	    }
	    
	    function addData(data, thisTable){
	    	if(thisTable){
	    		thisTable.fnAddData(data);
	    	}else{
	    		$bip.showMsg("向DataTables中添加数据异常");
	    	}
	    }

	    var $async = require("bip.async");

	    function bindDetailAndOperatorEvent(tableID, operatorArray, showDetailCallback, showDetailTmpName, thisTable){	    	
	    	if(showDetailCallback !== undefined && showDetailTmpName !== undefined){
	    		$("#" + tableID).on( "click", "tbody td .bip-detail", function(){
			       	var nTr = $(this).parents('tr')[0];
			       	if(thisTable.fnIsOpen(nTr)){
						$(this).addClass("icon_open").removeClass("icon_close");
						thisTable.fnClose(nTr);
					}else{
						$(this).addClass("icon_close").removeClass("icon_open");
						$.when(
				                $async.asyncGetTmpl(showDetailTmpName)
				         )
				         .done(function(tmplRes){
				                 var html = showDetailCallback.call(null, showDetailTmpName, tmplRes, nTr, thisTable);
				                 thisTable.fnOpen(nTr, html ,'details');
				        });
					}
		       });
	    	}
	    	
	    	if(operatorArray !== undefined && operatorArray !== null){
	    		$("#" + tableID).on( "click", "tbody td .bip-operator", function(){
		    		var nTr = $(this).parents('tr')[0];
		    		var aData = thisTable.fnGetData( nTr );
		    		
		    		var name = $(this).attr("name");
		    		
		    		$.each(operatorArray, function(index, item){
		    			if(name === item[0]){
		    				item[2].call(null, nTr, thisTable, aData);
		    			}
		    		});
		    	});
	    	}
	    	
	    	$("#" + tableID).on( "keyup",  "thead input", function () {
	            /* Filter on the column (the index) of this element */
	    		
	    		var index = parseInt($(this).attr("data"));
	    		
	    		thisTable.fnFilter( this.value, index);
	        } );
	    	
	    	$("#" + tableID).find("thead input").each(function(){
	    	  this.placeholderVal = this.placeholder;
	    	});
	    	
	    	$("#" + tableID).on( "focus","thead input", function () {
	          if(this.placeholderVal == this.placeholder){
	              this.placeholder = "";
	          }
	    	} );
	    	
	    	$("#" + tableID).on("blur","thead input",  function () {
	          if(this.placeholder == ""){
	            this.placeholder = this.placeholderVal;
	          }
	    	}); 	    	
	    }
	    
	    
	    
	    var uiDTableTool = {
	    		extDetail : extDetailToHeader,
	    		extOp : extOpToTail,
	    		extDetailAndOp : extDetailAndOp,
	    		initDTable : initDataTable,
	    		addData : addData
	    };
	    
	    return uiDTableTool;
	}
);